//
//  STCommentApi.swift
//  Story
//
//  Created by 汤泽川 on 2019/4/24.
//  Copyright © 2019 汤泽川. All rights reserved.
//

import Foundation

class STCommentApi {
    static public func sendComment(replayId : Int?, storyId : Int?, content : String, result : @escaping (_ response : ResponseModel<STCommentInfo>?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<STCommentInfo> >()
        request.requestPath = "/comment/send"
        if replayId != nil {
            request.requestParam = ["replayId" : replayId!, "content" : content]
        } else {
            request.requestParam = ["storyId" : storyId!, "content" : content]
        }
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.isSuccess {
                    result(data, nil)
                } else {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
    
    static public func queryComment(replayId : Int?, storyId : Int?, page : Int, count : Int, result : @escaping (_ response : ResponseModel<[STCommentInfo]>?, _ error : STError?) -> Void) {
        let request = STRequest<ResponseModel<[STCommentInfo]> >()
        request.requestPath = "/comment/query"
        if replayId != nil {
            request.requestParam = ["replayId" : replayId!, "page" : page, "count" : count]
        } else {
            request.requestParam = ["storyId" : storyId!, "page" : page, "count" : count]
        }
        request.sendRequest { (state) in
            switch state {
            case .success(let data):
                if data.isSuccess {
                    result(data, nil)
                } else {
                    result(nil, STError(code: data.status, reason: data.message, errorType: .customError))
                }
            case .fail(let error):
                result(nil, error)
            }
        }
    }
    
}
